<template>
    <div class="register">
        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form"
                 :style="{'width':this.isPhone ? '300px': ''}">
            <!--      <h3 class="title">智慧农业系统</h3>-->
            <h3 class="title">注册账号</h3>
<!--            <el-form-item prop="username">-->
<!--                <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="账号">-->
<!--                    <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>-->
<!--                </el-input>-->
<!--            </el-form-item>-->
            <el-form-item prop="companyLeaderPhone">
                <el-input v-model="registerForm.mobile" type="text" auto-complete="off" placeholder="手机号码">
                    <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon"/>
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input
                    v-model="registerForm.password"
                    type="password"
                    auto-complete="off"
                    placeholder="密码"
                    @keyup.enter.native="handleRegister"
                >
                    <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
                </el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword">
                <el-input
                    v-model="registerForm.confirmPassword"
                    type="password"
                    auto-complete="off"
                    placeholder="确认密码"
                    @keyup.enter.native="handleRegister"
                >
                    <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
                </el-input>
            </el-form-item>


            <el-form-item prop="code">
                <el-input v-model="registerForm.code"
                          auto-complete="off"
                          placeholder="输入验证码"
                          style="width: 63%"
                          @keyup.enter.native="handleRegister">
                    <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
                </el-input>
                <div class="register-code">
                    <el-button type="primary" plain :disabled="!phoneCodeStatus" @click="this.getPhoneCode"
                               style="text-align: center">
                        <span v-show="phoneCodeStatus">验证码</span>
                        <span v-show="!phoneCodeStatus">{{ countdownTime }}秒</span>
                    </el-button>
                </div>
            </el-form-item>

            <el-form-item style="width:100%;">
                <el-button
                    :loading="loading"
                    size="medium"
                    type="primary"
                    style="width:100%;"
                    @click.native.prevent="handleRegister"
                >
                    <span v-if="!loading">注 册</span>
                    <span v-else>注 册 中...</span>
                </el-button>
                <div style="float: right;">
                    <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
                </div>
            </el-form-item>
        </el-form>
        <!--  底部  -->
        <div class="el-register-footer">
            <!--      <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>-->
        </div>
    </div>
</template>

<script>
import {getCodeImg, register, getPhoneCode} from '@/api/login'

export default {
    name: 'Register',
    data() {
        const equalToPassword = (rule, value, callback) => {
            if (this.registerForm.password !== value) {
                callback(new Error('两次输入的密码不一致'))
            } else {
                callback()
            }
        }
        return {
            countdownTime: null,
            phoneCodeStatus: true,
            codeUrl: '',
            registerForm: {
                password: '',
                confirmPassword: '',
                mobile: null,
                code: null
            },
            registerRules: {
                username: [
                    {required: true, trigger: 'blur', message: '请输入您的账号'},
                    {min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur'}
                ],
                password: [
                    {required: true, trigger: 'blur', message: '请输入您的密码'},
                    // { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
                ],
                confirmPassword: [
                    {required: true, trigger: 'blur', message: '请再次输入您的密码'},
                    {required: true, validator: equalToPassword, trigger: 'blur'}
                ],
                mobile: [
                    {required: true, message: '请输入手机号码', trigger: 'blur'},
                    {min: 11, max: 11, message: '请输入11位长度的手机号码', trigger: 'blur'},
                    {
                        pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
                        message: '请输入正确的手机号码'
                    }
                ],
                code: [
                    {required: true, message: '请输入验证码', trigger: 'blur'},
                    {min: 4, max: 4, message: '请输入4位长度的验证码', trigger: 'blur'}
                ]
            },
            loading: false,
            captchaOnOff: true,
            isPhone: false
        }
    },
    created() {

        this.isPhone = this._isMobile()

        // this.getCode()
    },
    methods: {

        _isMobile() {
            let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
            return flag
        },

        // getCode() {
        //   getCodeImg().then(res => {
        //     this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        //     if (this.captchaOnOff) {
        //       this.codeUrl = "data:image/gif;base64," + res.img;
        //       this.registerForm.uuid = res.uuid;
        //     }
        //   });
        // },
        // 获取手机验证码
        getPhoneCode() {
            let pattern = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
            if (pattern.test(this.registerForm.mobile)) {
                let scene = process.env.VUE_APP_REGISTER_SCENE
                let obj = {
                    scene:Number(scene),
                    mobile:this.registerForm.mobile
                }
                getPhoneCode(obj).then(response => {
                    if (response.data) {
                        this.$message.success('发送成功')

                        this.phoneCodeStatus = false
                        // 30秒
                        this.countdownTime = 30
                        const timeInterval = setInterval(() => {
                            this.countdownTime--
                            if (this.countdownTime <= 0) {
                                this.phoneCodeStatus = true
                                clearInterval(timeInterval)
                            }
                        }, 1000)
                    }
                    // if (response.code === 600) {
                    //   this.$message.error("1")
                    // }

                }, error => {
                })
            } else {
                this.$message.error('请输入正确的手机号')
            }
        },
        handleRegister() {
            this.$refs.registerForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    register(this.registerForm).then(res => {
                        const mobile = this.registerForm.mobile
                        this.$alert('<font color=\'red\'>账号 ' + mobile + ' 注册成功！</font>', '系统提示', {
                            dangerouslyUseHTMLString: true,
                            type: 'success'
                        }).then(() => {
                            this.$router.push('/login')
                        }).catch(() => {
                        })
                    }).catch(() => {
                        this.loading = false
                    })
                }
            })
        }
    }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.register {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/images/login-background2.jpg");
    background-size: cover;
}

.title {
    margin: 0 auto 30px auto;
    text-align: center;
    color: #707070;
}

.register-form {
    border-radius: 6px;
    //background: rgba(255, 255, 255, 0.7); /* 半透明背景 */
    background-color: #ffffff; /* 半透明背景 */
    width: 400px;
    padding: 25px 25px 5px 25px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
    .el-input {
        height: 38px;

        input {
            height: 38px;
        }
    }

    .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 2px;
    }
}

.register-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
}

.register-code {
    width: 33%;
    height: 38px;
    float: right;

    .el-button {
        width: 100%;
    }

    img {
        cursor: pointer;
        vertical-align: middle;
    }
}

.el-register-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
}

.register-code-img {
    height: 38px;
}
</style>
